package com.bw.services.activity

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bw.common.mvvm.BaseViewActivity
import com.bw.lib_base.base.BaseActivity
import com.bw.services.R
import com.bw.services.databinding.ActivityUpDataParkingBinding
import com.bw.services.entity.CarParkingEntity
import com.bw.services.viewmodel.ServicesViewModel
import com.therouter.router.Route
import dagger.hilt.android.AndroidEntryPoint
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ToastUtils
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = "app/UpDataParkingActivity")
class UpDataParkingActivity : BaseActivity<ActivityUpDataParkingBinding, ServicesViewModel>() {
    private lateinit var parkingItem: CarParkingEntity
    private var selectedStatus: String = "空闲"
    
    override fun generatePremission(pageName: String): Array<String> {
        TODO("Not yet implemented")
    }

    override fun setupUI() {
        // 接收传递的数据
        parkingItem = intent.getSerializableExtra("parkingItem") as? CarParkingEntity ?: return
        
        // 赋值到控件
        binding.etNumber.setText(parkingItem.spaceNumber)
        binding.actvArea.setText(parkingItem.areaName)
        binding.actvType.setText(parkingItem.spaceType)
        binding.etUser.setText(parkingItem.userName)
        binding.etPlate.setText(parkingItem.licensePlate)
        binding.etRemark.setText(parkingItem.remarks)
        
        // 设置初始状态
        selectedStatus = parkingItem.spaceStatus
        
        // 车位状态按钮高亮
        val statusButtons = listOf(binding.btnStatusFree, binding.btnStatusOccupied, binding.btnStatusMaintain)
        fun updateStatusButtons(selected: Int) {
            statusButtons.forEachIndexed { idx, btn ->
                btn.isSelected = idx == selected
                btn.setTextColor(if (idx == selected) 0xFF4D6AFF.toInt() else 0xFF222222.toInt())
            }
        }
        
        // 根据传入状态设置初始高亮
        when (parkingItem.spaceStatus) {
            "空闲" -> updateStatusButtons(0)
            "已占用" -> updateStatusButtons(1)
            "维护中" -> updateStatusButtons(2)
            else -> updateStatusButtons(0)
        }
        
        // 设置按钮点击事件
        statusButtons.forEachIndexed { idx, btn ->
            btn.setOnClickListener {
                updateStatusButtons(idx)
                selectedStatus = when(idx) {
                    0 -> "空闲"
                    1 -> "已占用"
                    2 -> "维护中"
                    else -> "空闲"
                }
            }
        }
        
        // 保存按钮点击事件
        binding.btnSave.setOnClickListener {
            // 收集控件内容
            val upData = parkingItem.copy(
                spaceNumber = binding.etNumber.text.toString(),
                areaName = binding.actvArea.text.toString(),
                spaceType = binding.actvType.text.toString(),
                userName = binding.etUser.text.toString(),
                licensePlate = binding.etPlate.text.toString(),
                remarks = binding.etRemark.text.toString(),
                spaceStatus = selectedStatus
            )
            lifecycleScope.launch { vm.getUpData_Parking(parkingItem.id, upData) }

        }
    }

    override fun fetchData() {

    }

    override fun observeData() {
        lifecycleScope.launch {
            vm.upDataParking.filterNotNull().collect {
                    ToastUtils.showShort("修改成功")
                    finish()
            }
        }
    }

    override fun handleEvent() {

    }

    override val mLayoutResId: Int
        get() = TODO("Not yet implemented")

}